<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { userLoginService, userRegisterService } from '@/api/user.js'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores'

// model 属性绑定 form 数据对象
const formModel = ref({
  phone: '',
  password: ''
})
// rules 配置校验规则
const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    {
      pattern: /^1[3456789]\d{9}$/,
      message: '手机号格式不正确',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{8,16}$/,
      message: '密码必须为8到16位，只能包含大小写字母和数字',
      trigger: 'blur'
    }
  ]
}
const form = ref()
const userStore = useUserStore()
const router = useRouter()

const isRegister = ref(false)

const login = async () => {
  await form.value.validate()
  let res
  if (!isRegister.value) {
    res = await userLoginService(formModel.value)
    ElMessage.success('登录成功')
    userStore.token = res.data.data.token
    userStore.user = res.data.data
    userStore.phone = formModel.value.phone
    userStore.password = formModel.value.password
    console.log(formModel.value)

    router.push('/')
  } else {
    res = await userRegisterService(formModel.value)
    ElMessage.success('注册成功，请登录')
    isRegister.value = false
    formModel.value = {
      phone: '',
      password: ''
    }
  }
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        :model="formModel"
        :rules="rules"
      >
        <el-form-item>
          <h1>{{ isRegister ? '注册' : '登录' }}</h1>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
            :prefix-icon="User"
            placeholder="请输入手机号"
            v-model="formModel.phone"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item> -->
        <el-form-item>
          <el-button
            class="button"
            type="primary"
            auto-insert-space
            @click="login"
            >{{ isRegister ? '注册' : '登录' }}</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link
            type="info"
            :underline="false"
            @click="isRegister = !isRegister"
          >
            {{ isRegister ? '返回登录' : '注册 →' }}
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background:
      url('@/assets/logo.png') no-repeat 50% center / 200px auto,
      url('@/assets/login_bg.png') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
